indusfandomcom-20200214-history
Methods
2.3.3 Method declarations A method declares an executable port that can be invoked, passing a fixed number of values as arguments. MethodDeclaration: MethodHeader MethodBody MethodHeader: MethodModifiers (opt) ResultType MethodDeclarator Throws (opt) where : ResultType: Type Void MethodDeclarator: Identifier (formal parameter list optional) FormalParameter: Final (opt) Type VariableDeclaratorID There is a compile-time error : If the body of a class have two methods with the same signature. If two formal parameters of the same method are declared to have the same name, then a compile-time error occurs. If a method parameter that is declared final is assigned to within the body of the method. A constructor is also treated as a method. Parameter names may not be re-declared as local variables of the method, or as exception parameters of catch clauses in a try statement of the method. 2.3.3.2 Method modifiers Method modifiers are one of the following; if two or more method modifiers appear in a method declaration, it is customary, though not required, that they appear in the order consistent with that shown below: Public Protected Private Abstract Static Final Synchronized A compile-time error occurs : If the same modifier occurs more than once in the method declaration If a method declaration has more than one of the access modifiers : public, protected or private. If a method declaration contains the keyword abstract and also contains any of the keywords, private, static, final, native or synchronized. An abstract method declaration introduces the method as a member, providing its signature, return type and throws clause (if any), but does not provide an implementation. A declaration of a abstract method must appear directly within an abstract class, failing which a compile-time error results. An abstract class can override an abstract method by providing another method declaration. A method that is declared static is always invoked without reference to a particular object. An attempt to reference the current object using the keyword this or the keyword super in the body of a static method results in a compile-time error. A static method cannot be declared abstract. A non-static method can always refer to the current object using the keywords this and super during the execution of the method body. A method can be declared final to prevent subclasses from overriding or hiding it. A private method and all methods declared in a final class is implicitly final as it is impossible to override them. “A final method cannot be declared abstract. “ A throws clause is used to declare any checked exceptions that can result from the execution of a method. A compile-time error occurs if any class type mentioned in a throws clause is not the class throwable or a subclass throwable. A method body is either a block of code that implements a method or simply a semi-colon indicating the lack of implementation such as in cases abstract and native methods. If a method is declared void, then its body must not contain any return statement that has a expression. If a method is declared to have return type, then every return statement in its body must have an expression. A class inherits from its direct super class all the non-private methods (whether abstract or not) of the super class that are accessible to code in the class and are neither overridden nor hidden by a declaration in the class. 2.3.3.3 Inheritance, Overriding, and Hiding A class C inherits from its direct superclass and direct superinterfaces all non-private methods (whether abstract or not) of the superclass and superinterfaces that are public, protected or declared with default access in the same package as C and are neither overridden nor hidden by a declaration in the class. An instance method m1 declared in a class C overrides another instance method, m2, declared in class A iff all of the following are true: C is a subclass of A. The signature of m1 is a subsignature of the signature of m2. Either m2 is public, protected or declared with default access in the same package as C, or m1 overrides a method m3, m3 distinct from m1, m3 distinct from m2, such that m3 overrides m2. Moreover, if m1 is not abstract, then m1 is said to implement any and all declarations of abstract methods that it overrides. A compile-time error occurs if an instance method overrides a static method. It is a compile time error if a type declaration T has a member method m1 and there exists a method m2 declared in T or a supertype of T such that all of the following conditions hold: m1 and m2 have the same name. m2 is accessible from T. The signature of m1 is not a subsignature of the signature of m2. m1 or some method m1 overrides (directly or indirectly) has the same erasure as m2 or some method m2 overrides (directly or indirectly).